<?php

namespace Home\Controller;

use Think\Controller;

class AboutBuildingController extends Controller {

    public function index() {
        $aboutBuildingObj = M('aboutbuilding');
        $condition['PublicCodeID'] = 1;
        $aboutBuilding = $aboutBuildingObj->where($condition)->find();
        if ($aboutBuilding) {
            $aboutBuilding['UpdateTime'] = date('Y-m-d H:i:s', $aboutBuilding['UpdateTime']);
        }

        if ($aboutBuilding != null) {
            $this->assign('vo', $aboutBuilding);
        }
        $this->display("AboutBuilding");
    }

    public function uploadImage() {
        //$callbackName = $_GET['CKEditorFuncNum'];
        //$callbackName = I('CKEditorFuncNum', '', "htmlspecialchars");
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->maxSize = 2097152; // 设置附件上传大小
        $upload->rootPath = './Uploads/Images/'; // 设置附件上传根目录
        $upload->exts = array('bmp', 'png', 'jpeg', 'jpg', 'gif'); // 设置附件上传类型
        $info = $upload->uploadOne($_FILES['imgFile']);
        $result['error'] = 0;
        $result['url'] = "../../Uploads/Images/" . $info['savepath'] . $info['savename'];
        $json = json_encode($result, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

    public function saveAboutBuildingHTML() {
        $htmlData = I('htmlData');
        $HTMLContentID = I('htmlDataID', '', 'htmlspecialchars');
        $htmlData = htmlspecialchars_decode($htmlData);
        //$clean_HTML=$htmlData;
        $clean_HTML = HTMLFilter($htmlData);
        //////////////////////////////////////////////////////////
        //Edit by Ron.shen 此处经过防XSS处理过后存入mysql出现了换行和空格不能存入，数据库中显示为？，但是HTML中标签中又可能会存在空格，所以
        //                  不能全部替换空格，试验以后发现需要单独修改数据表中该列编码为utf8_unciode_ci，所以此处附上修改单列编码的SQL语句
        //                  ALTER TABLE  `sh_aboutbuilding` CHANGE  `HtmlContent`  `HtmlContent` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL
        //////////////////////////////////////////////////////////
//        $clean_HTML = preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "", $clean_HTML); //去除空格,此处不能依靠HTMLPurifier去除空标签，否则会把整个标签都去除掉
        $clean_HTML = str_replace("<em>", "<i>", $clean_HTML);
        $clean_HTML = str_replace("</em>", "</i>", $clean_HTML);
        $aboutBuildingObj = M('aboutbuilding');
        if ($HTMLContentID != '') {
            $aboutBuildingData['ID'] = $HTMLContentID;
        }
        $aboutBuildingData['HtmlContent'] = $clean_HTML;
        $aboutBuildingData['UpdateTime'] = time();
        $aboutBuildingData['PublicCodeID'] = 1;

        $ID = $aboutBuildingObj->add($aboutBuildingData, array(), true);
        $result = array();
        if ($ID) {
            $result['error'] = 0;
            $result['ID'] = $ID;
        } else {
            $result['error'] = 1;
        }
        $json = json_encode($result, JSON_UNESCAPED_UNICODE);
        $this->ajaxReturn($json, 'EVAL');
    }

}
